﻿using Avin.Infrastructure.DbContext;
using Avin.Infrastructure.Entity;
using System.Collections.Generic;
using System.Data.Common;

namespace Avin.Infrastructure.Linq
{
    internal static class ToData
    {
        internal static int Count(IDbContextBase dbContextBase, string sql, DbParameter[] dbParameters)
        {
            object obj = dbContextBase.ExecuteScalar(sql, dbParameters);
            if (obj == null)
                return 0;

            return int.Parse(obj.ToString());
        }

        internal static T FirstOrDefault<T>(IDbContextBase dbContextBase, string sql, DbParameter[] dbParameters)
        {
            return dbContextBase.ExecuteDataSet(sql, dbParameters).ToSingle<T>();
        }

        internal static T SingleOrDefault<T>(IDbContextBase dbContextBase, string sql, DbParameter[] dbParameters)
        {
            return dbContextBase.ExecuteDataSet(sql, dbParameters).ToSingle<T>();
        }

        internal static List<T> ToList<T>(IDbContextBase dbContextBase, string sql, DbParameter[] dbParameters)
        {
            return dbContextBase.ExecuteDataSet(sql, dbParameters).ToList<T>();
        }
    }
}
